package com.meowcook.service.impl;

import com.meowcook.common.AppResult;
import com.meowcook.common.ResultCode;
import com.meowcook.dao.FollowMapper;
import com.meowcook.dao.UserMapper;
import com.meowcook.exception.AppException;
import com.meowcook.mapper.FollowPojoMapper;
import com.meowcook.pojo.dto.FollowDTO;
import com.meowcook.pojo.po.Follow;
import com.meowcook.pojo.po.User;
import com.meowcook.pojo.vo.FollowVO;
import com.meowcook.pojo.vo.IdolVO;
import com.meowcook.service.ISocietyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-10-27
 * Time: 22:19
 */
@Slf4j
@Service
public class SocietyServiceImpl implements ISocietyService {

    @Resource
    private FollowPojoMapper followPojoMapper;

    @Resource
    private FollowMapper followMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void followOrNot(FollowDTO followDTO) {
        // 参数校验
        Follow follow = followPojoMapper.toPO(followDTO);
        Long fanId = follow.getFanId();
        Long idolId = follow.getIdolId();
        if (fanId.equals(idolId)) {
            throw new AppException(AppResult.fail("不能关注自己！"));
        }
        User fan = userMapper.selectByPrimaryKey(fanId);
        User idol = userMapper.selectByPrimaryKey(idolId);
        if (idol == null || idol.getIsDeleted() == 1) {
            throw new AppException(AppResult.fail("所关注的用户不存在！"));
        }
        // id不为空说明不是第一次关注
        if (follow.getId() != null) {
            follow.setUpdateTime(new Date());
            int row = followMapper.updateByPrimaryKeySelective(follow);
            if (row != 1) {
                throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
            }
        } else {
            // id为空则添加关注
            follow.setUpdateTime(new Date());
            follow.setCreateTime(new Date());
            int row = followMapper.insertSelective(follow);
            if (row != 1) {
                throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
            }
        }
        // 更新用户信息
        int followCount = fan.getFollowCount();
        int fanCount = idol.getFansCount();

        if (follow.getIsBlocked() == 1) {
            followCount -= 1;
            fanCount -=  1;
        } else {
            followCount += 1;
            fanCount +=  1;
        }

        fan = new User();
        fan.setId(fanId);
        fan.setFollowCount(followCount);
        int row = userMapper.updateByPrimaryKeySelective(fan);
        if (row != 1) {
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }

        idol = new User();
        idol.setId(idolId);
        idol.setFansCount(fanCount);
        row = userMapper.updateByPrimaryKeySelective(idol);
        if (row != 1) {
            throw new AppException(AppResult.fail(ResultCode.ERROR_SERVICE));
        }
    }

    @Override
    public List<IdolVO> getIdolList(Long fanId) {
        if (fanId == null) {
            throw new AppException(AppResult.fail("用户ID不能为空"));
        }
        List<Follow> idols = followMapper.getIdolList(fanId);
        return followPojoMapper.toIdolVO(idols);
    }

    @Override
    public FollowVO getFollowInfo(Long fanId, Long idolId) {
        if (fanId.equals(idolId)) {
            throw new AppException(AppResult.fail("无法查看自己关注自己的记录"));
        }
        User user = userMapper.selectByPrimaryKey(idolId);
        if (user == null || user.getIsDeleted() == 1) {
            throw new AppException(AppResult.fail("查询的用户不存在！"));
        }
        Follow follow = followMapper.selectByFanIdAndIdolId(fanId, idolId);
        return followPojoMapper.toVO(follow);
    }
}
